package com.service.impl;

import com.mapper.NoteProductsMapper;
import com.pojo.NoteProducts;
import com.pojo.Result;
import com.service.NoteProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoteProductsServiceImpl implements NoteProductsService {

    @Autowired
    private NoteProductsMapper noteProductsMapper;

    @Override
    public Result<Void> addProductToNote(Integer noteId, Integer productId) {
        try {
            // 检查关联是否已存在
            if (noteProductsMapper.exists(noteId, productId) > 0) {
                return Result.error("该产品已关联到此笔记");
            }

            // 创建关联
            NoteProducts noteProduct = new NoteProducts();
            noteProduct.setNoteId(noteId);
            noteProduct.setProductId(productId);
            noteProduct.setCreatedAt(LocalDateTime.now());

            int result = noteProductsMapper.insert(noteProduct);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("添加产品到笔记失败");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeProductFromNote(Integer noteId, Integer productId) {
        try {
            int result = noteProductsMapper.delete(noteId, productId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("移除产品失败，可能关联不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeAllProductsFromNote(Integer noteId) {
        try {
            int result = noteProductsMapper.deleteByNoteId(noteId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有产品关联也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> removeProductFromAllNotes(Integer productId) {
        try {
            int result = noteProductsMapper.deleteByProductId(productId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有关联也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Integer>> getProductsByNoteId(Integer noteId) {
        try {
            List<Integer> productIds = noteProductsMapper.selectProductIdsByNoteId(noteId);
            return Result.success(productIds);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Integer>> getNotesByProductId(Integer productId) {
        try {
            List<Integer> noteIds = noteProductsMapper.selectNoteIdsByProductId(productId);
            return Result.success(noteIds);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Boolean> isProductAttachedToNote(Integer noteId, Integer productId) {
        try {
            int exists = noteProductsMapper.exists(noteId, productId);
            return Result.success(exists > 0);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}